#!/bin/bash -e

function info() {
echo Usage: `basename $0` 'bam'
exit 1
}

while getopts  ":p:f:" opt; do
	case  $opt  in
		p) out_prefix=$OPTARG;;
		f) suffix=$OPTARG;;
		*) info;;
	esac
done
shift $(($OPTIND - 1))


if [ $# -lt 1 ]; then info; fi

. $var

capture_bed=$data_path/intervals/1/capture.bed

capture_pos=$data_path/intervals/1/capture.bed.pos

m1_bed=$data_path/intervals/1/b37.1m.bed

bam=$1

bedtools coverage -d -split -hist -abam $bam -b $capture_bed > $out_prefix.bam.depth

cut -f8 $out_prefix.bam.depth > $out_prefix.bam.depth.8

#-----------------------------
# paste capture.bed.pos $out_prefix.bam.depth.8 > capture.cov.txt

#------------------------------

# bedtools makewindows -b /mnt/ilustre/app/medical/.data/bed/b37.bed -w 1000000 > $data_path/intervals/1/b37.1m.bed

sort_idx_sam.sh $bam
samtools bedcov $m1_bed $out_prefix.sort.bam |sort -k4,4nr > $out_prefix.read.cov.1m.txt

# bedtools coverage -split -hist -abam $bam -b $m1_bed > $out_prefix.bam.depth.1m

#---------------------------------------

# cat *.depth.cg.txt|cut -f1,3 > depth.cg
# sort depth.cg|uniq -c|sed 's/^  *//g; s/ /\t/'|sort -k1,1nr > id.rep.cg
# cut -f3 id.rep.cg > cg.txt
